package phase1.part2;

/**
 * A Transition contains the end state and pattern of a state's transition.
 * 
 * @author Devin Wang
 * @version Nov 13, 2012
 */
public class Transition {
	private State end;
	private Pattern pattern;
	
	/**
	 * Create a Transition.
	 * @param end The transition's end state
	 * @param pattern The transition's pattern
	 */
	public Transition(State end, Pattern pattern) {
		this.end = end;
		this.pattern = pattern;
	}
    
	/**
	 * Add a character to the transition's pattern.
	 * @param c The character to add
	 */
    public void add(char c){
        pattern.add(c);
    }
    
    /**
     * Checks whether or not a character is in the transition's pattern.
     * @param c The character to check for
     * @return <CODE>true</CODE> if the character is in the patter, <CODE>false</CODE> if not
     */
    public boolean accepts(char c){
    	if(pattern == null){
    		return false;
    	}
        return pattern.accepts(c);
    }
    
    /**
     * Get the transition's pattern.
     * @return pattern
     */
    public String getPattern(){
        return pattern.getPattern();
    }
    
    /**
     * Set the transition's pattern.
     * @param pattern The new pattern
     */
    public void setPattern(Pattern pattern){
    	this.pattern = pattern;
    }
    
    /**
     * Check if the transition is an e-Transition.
     * @return <CODE>true</CODE> if the transition has no pattern, <CODE>false</CODE> if it does.
     */
    public boolean isETransition(){
        return (pattern == null);
    }

    /**
     * Get the transition's end state.
     * @return end
     */
	public State getEnd() {
		return end;
	}

	/**
	 * Set the transition's end state.
	 * @param end The new end
	 */
	public void setEnd(State end) {
		this.end = end;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Transition))
			return false;
		Transition other = (Transition) obj;
		if (end == null) {
			if (other.getEnd() != null)
				return false;
		} else if (!end.equals(other.getEnd()))
			return false;
		if (pattern == null) {
			if (other.getPattern() != null)
				return false;
		} else if (!pattern.equals(other.getPattern()))
			return false;
		return true;
	}

	@Override
	public String toString() {
        if(pattern == null){
            return "Transition [end=" + end + ", no pattern]";
        }
		return "Transition [end=" + end + ", pattern=" + pattern + "]";
	}
}